import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.JTextPane;


public class ThreadStartDeploy extends Thread {
	JTextArea panel;
	 JButton b;
	 String fileName;
	Process p;
	int nTask;
	JProgressBar progress;
	List<String> progetti = new ArrayList<String>();
	boolean scriviLog;
	
	
	public ThreadStartDeploy(String fileName,JTextArea panel, JButton b,JProgressBar progress, int nTask, boolean scriviLog)
	{
		this.panel = panel;
		this.b = b;
		this.fileName= fileName;
		this.progress= progress;
		this.nTask= nTask;
		this.scriviLog= scriviLog;
		
		panel.setText("");
		progetti.add("BatchEngine");
		progetti.add("BatchAppWar");
		progetti.add("BatchApp");
		progetti.add("BatchAppEar");
		progetti.add("RgiService");
		progetti.add("BatchWrap");
		progetti.add("PassHibernate");
		progetti.add("SessionPool");
		progetti.add("FlowsShare");
		progetti.add("FlowsHibernate");
		progetti.add("WSClient");
		progetti.add("FlowsParser");
		progetti.add("Common");
		progetti.add("FlowsAnia");
		progetti.add("FlowsEngine");
		progetti.add("FlowsLogParser");
		progetti.add("FlowsOperationWar");
		progetti.add("FlowsOperationEar");
		progetti.add("FlowsOperation");
		progetti.add("FlowsSchedulerWeb");
		progetti.add("FlowsCommon");
	}
	
	public void run() {
		try {
			PrintWriter writer = null;
			if( scriviLog )
				 writer = new PrintWriter("log.txt", "UTF-8");
			
			
			int prog = -1;
			int i = 0;
			b.setEnabled(false);
			double stepProgresso = 100/nTask;
			double progresso = 0;
			//ProcessBuilder pb = new ProcessBuilder("executeDeploy.bat");
			ProcessBuilder pb = new ProcessBuilder(fileName);
			p = pb.start(); 
			progress.setValue(0);
			// process runs in another thread parallel to this one
			BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line = "";
/*Building*/
			while ((line = bri.readLine()) != null) {
				panel.setText(panel.getText()+"\n"+line);
				panel.setCaretPosition(panel.getText().length());
				if( line.contains("Replaced deployment")  ||  line.contains("Deployed \"FlowsSchedulerWeb")  )
					Runtime.getRuntime().exec(new String[] { "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "http://localhost:8080/FlowsSchedulerWeb/" });
				if( contieneProg(line) )
				{
					prog++;
					progresso = progresso + stepProgresso;
					progress.setValue((int)progresso);
					
				}
				if( scriviLog )
					writer.println(line);
				//writer.println("The second line");
				if(panel.getText().length() > 50000)
					panel.setText(panel.getText().substring(0, 1000));
				i++;
					
			}
			if( scriviLog )
				writer.close();
			progress.setValue(100);
			
			b.setEnabled(true);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public boolean contieneProg(String linea)
	{
		
		for (Iterator iterator = progetti.iterator(); iterator.hasNext();) {
			String progetto = (String) iterator.next();
			if( linea.contains("[INFO] Building "+progetto) )
				return true;
		}
		return false;
	}
	public void destroyProcess()
	{
		p.destroy();
		b.setEnabled(true);
	}

}
